package com.glory.wxelectricitypda.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.activity.PackPackageActivity;
import com.glory.wxelectricitypda.adapter.PackListAdapter;
import com.glory.wxelectricitypda.bean.PackBean;
import com.glory.wxelectricitypda.bean.ResponseAfterPackBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NotDoneFragment extends FreeUI_BaseFragment {

    public PackListAdapter packListAdapter;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.llMain)
    FrameLayout llMain;
    @BindView(R.id.checkAll)
    CheckBox checkAll;

    public NotDoneFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_not_done;
    }

    @Override
    protected void initView() {
        rvThingList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(getActivity(), 1));

        packListAdapter = new PackListAdapter(null);
        packListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        packListAdapter.isFirstOnly(true);
        rvThingList.setAdapter(packListAdapter);
    }

    @Override
    protected void initData() {
        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                packListAdapter.setAll(isChecked);
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    @OnClick({R.id.tvPack,R.id.tvGlePack})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvPack:
                euitpacking(false);
                break;
            case R.id.tvGlePack:
                euitpacking(true);
                break;
        }
    }

    private void euitpacking(boolean b) {
        if (!b || ((PackPackageActivity) getActivity()).packType.equals("BoxPackageType")){
            if (!((PackPackageActivity) getActivity()).isBoxPackageType) {
                showToast("请先完成校验", false);
                return;
            }
        }

        final List<PackBean> selectList = new ArrayList<>();
        for (PackBean packBean : packListAdapter.getData()) {
            if (packBean.isSelect()) {
                selectList.add(packBean);
            }
        }
        if (selectList.size() == 0) {
            showToast("请选择要包装的批次", false);
            return;
        }
//        if (selectList.size() > 10) {
//            showToast("最多只能选择10条批次", false);
//            return;
//        }
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            String packType = ((PackPackageActivity) getActivity()).packType;
            jsonObject1.put("ACTIONTYPE", packType.equals("ReelPack") ? "ReelPack" : "Pack");
            jsonObject1.put("PACKAGETYPE", ((PackPackageActivity) getActivity()).packType
                    .equalsIgnoreCase("BoxPackageType") ? "BoxPackageType" : "SmallBoxPackageType");
            JSONArray jsonArray = new JSONArray(new Gson().toJson(selectList));
            jsonObject1.put("LOTLIST", jsonArray);
            jsonObject1.put("PACKEDLOTID", "");
            jsonObject1.put("ISSINGLEPACKING", b);
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITPACKING");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<ResponseAfterPackBean>>(
                new TypeToken<ResponseBean<ResponseAfterPackBean>>() {
                }.getType(), getFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<ResponseAfterPackBean>> response) {
                if (response.body().isSuccess(getContext(), true, getActivity().getSupportFragmentManager())) {
                    List<PackBean> remainList = response.body().getBody().getREMAINLOTLIST();
                    if (remainList.size() > 0) {
                        showToast("包装成功，还存在尾批，请继续包装", true);
                        for (PackBean obj : remainList) {
                            for (PackBean packBean : selectList) {
                                if (packBean.getLOTID().equals(obj.getLOTID())) {
                                    packBean.setMAINQTY(obj.getMAINQTY());
                                }
                            }
                        }
                    } else {
                        packListAdapter.clearPos();
                        packListAdapter.getData().removeAll(selectList);
                        showToast("全部包装成功", true);
                    }
                    List<PackBean> alreadyList = response.body().getBody().getLOTLIST();
                    if (alreadyList.size() > 0) {
                        packListAdapter.getData().removeAll(alreadyList);
                        ((PackPackageActivity) getActivity()).setAlreadyDoneList(alreadyList);
                    }
                    packListAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Response<ResponseBean<ResponseAfterPackBean>> response) {
                super.onError(response);
                loadError(response.getException(), "包装请求");
            }
        });
    }

    public void addData(PackBean packBean){
        ((PackPackageActivity) getActivity()).isBoxPackageType = false;
        packListAdapter.addData(0, packBean);
        rvThingList.smoothScrollToPosition(0);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            ((PackPackageActivity) getActivity()).etLotId.setEnabled(true);
        }
    }
}
